package com.example.grape_utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.annotation.NonNull;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class MediaController {
    public static final String root_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
    public static final String image_tail = ".jpg";
    public static final String video_tail = ".mp4";
    public static final String image_mine_type = "image/jpeg";
    public static final String video_mine_type = "video/mp4";

    public static final SimpleDateFormat simple_data_format = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);

    public static Uri insertImage(Context context, String name, long data, int orientation, long size, String path, int width,
                                  int height, Location location) {

        ContentValues values = new ContentValues();
        values.put(MediaStore.Files.FileColumns.MEDIA_TYPE, MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
        values.put(MediaStore.Images.ImageColumns.TITLE, name);
        values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, name + image_tail);
        values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, data);
        values.put(MediaStore.Images.ImageColumns.MIME_TYPE, image_mine_type);
        values.put(MediaStore.Images.ImageColumns.ORIENTATION, orientation);
        values.put(MediaStore.Images.ImageColumns.SIZE, size);
        values.put(MediaStore.Images.ImageColumns.DATA, path);
        values.put(MediaStore.Images.ImageColumns.WIDTH, width);
        values.put(MediaStore.Images.ImageColumns.HEIGHT, height);
        values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, System.currentTimeMillis());

        if(location != null ) {
            values.put(MediaStore.Images.ImageColumns.LATITUDE, location.getLatitude());
            values.put(MediaStore.Images.ImageColumns.LONGITUDE, location.getLongitude());
        }
        ContentResolver resolver = context.getContentResolver();
        Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
        return uri;
    }

    public static Uri insertVideo(Context context, String name, long data, int size, String path, int width,
                                  int height, long duration, Location location) {

        ContentValues values = new ContentValues();
        values.put(MediaStore.Files.FileColumns.MEDIA_TYPE, MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO);
        values.put(MediaStore.Video.VideoColumns.TITLE, name);
        values.put(MediaStore.Video.VideoColumns.DISPLAY_NAME, name + video_tail);
        values.put(MediaStore.Video.VideoColumns.DATE_TAKEN, data);
        values.put(MediaStore.Video.VideoColumns.MIME_TYPE, video_mine_type);
        values.put(MediaStore.Video.VideoColumns.SIZE, size);
        values.put(MediaStore.Video.VideoColumns.DATA, path);
        values.put(MediaStore.Video.VideoColumns.WIDTH, width);
        values.put(MediaStore.Video.VideoColumns.HEIGHT, height);
        values.put(MediaStore.Video.VideoColumns.RESOLUTION, width + "x" + height);
        values.put(MediaStore.Video.VideoColumns.DURATION,duration);
        values.put(MediaStore.Video.VideoColumns.DATE_MODIFIED, System.currentTimeMillis());

        if(location != null ) {
            values.put(MediaStore.Video.VideoColumns.LATITUDE, location.getLatitude());
            values.put(MediaStore.Video.VideoColumns.LONGITUDE, location.getLongitude());
        }
        ContentResolver resolver = context.getContentResolver();
        Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
        return uri;
    }


    public static String generateImageName(){
        return "IMG-" + simple_data_format.format(System.currentTimeMillis());
    }

    public static String generateVideoName(){
        return "VID-" + simple_data_format.format(System.currentTimeMillis());
    }
}
